package org.jeecg.modules.cascv.image.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.jeecg.modules.cascv.image.entity.GdqvPara;
import org.jeecg.modules.cascv.image.mapper.GdqvParaMapper;
import org.jeecg.modules.cascv.image.mapper.ShiYanCangSqlDaoMapper;
import org.jeecg.modules.cascv.image.service.IGdqvParaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Description: xml参数表
 * @Author: jeecg-boot
 * @Date: 2022-08-30
 * @Version: V1.0
 */
@Service
public class GdqvParaServiceImpl extends ServiceImpl<GdqvParaMapper, GdqvPara> implements IGdqvParaService {

    @Autowired
    ShiYanCangSqlDaoMapper sqlDaoMapper;

    //工程 contype
    @Override
    public void gongchengXmlInsert(MultipartFile file, Integer type) {
        String fileName = file.getOriginalFilename();
        Integer id = sqlDaoMapper.xmlMaxId();
        if (null == id) {
            id = 1;
        } else {
            id = id + 1;
        }
        SAXReader reader = new SAXReader();
        Document read = null;
        String tableName = StringUtils.substringBefore(fileName, ".");
        Boolean aBoolean = sqlDaoMapper.xmlIs(tableName);

        if (aBoolean != null) {
            return;
        }
        try {
            InputStream inputStream = file.getInputStream();
            read = reader.read(inputStream);

        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("t", "IDSParameter");
        if (read == null) {
            return;
        }
        XPath x = read.createXPath("/t:IDS_Parameter_root/t:FileInfo");
        XPath x1 = read.createXPath("/t:IDS_Parameter_root/t:Parameters/t:Parameter");

        x.setNamespaceURIs(map);
        x1.setNamespaceURIs(map);

        List<Element> list = x.selectNodes(read);
        List<Element> listPara = x1.selectNodes(read);

        String EquipmentName = "";
        String SubsystemName = "";
        String EquipmentCode = "";
        String BaySection = "";

        Map<String, Object> addParaMap = new HashMap<>();

        for (Element ele : list) {
            EquipmentName = ele.elementText("EquipmentName");
            SubsystemName = ele.elementText("SubsystemName");
            EquipmentCode = ele.elementText("EquipmentCode");
            BaySection = ele.elementText("BaySection");
        }
        boolean is352EF853H = false;

        if (EquipmentName.contains("生命生态实验柜") || EquipmentName.contains("变重力实验柜") || EquipmentName.contains("生物技术实验柜")
                || EquipmentName.contains("科学手套箱与低温存储柜")) {
            for (Element element : listPara) {
                List<Element> BitvalueList = element.elements("ByteValue");
                for (Element ele : BitvalueList) {
                    String value = ele.elementText("Value");
                    if (value.equals("352EF853H")) {
                        is352EF853H = true;
                    }
                    break;
                }
                break;
            }
        }

/*            if (is352EF853H){
                continue;
            }*/
        addParaMap.put("ID", id);
        addParaMap.put("NAME", tableName);
        addParaMap.put("XML_TYPE", type);//遥测 type=1 工程type=2 图像type =3 科学type=5
        addParaMap.put("TS_NAME", tableName);
        addParaMap.put("DEL_FLAG", "0");
        addParaMap.put("EQUIPMENTNAME", EquipmentName);
        addParaMap.put("SUBSYSTEMNAME", SubsystemName);
        addParaMap.put("BAYSECTION", EquipmentCode);
        addParaMap.put("EQUIPMENTCODE", BaySection);
        addParaMap.put("CONTENT_TYPE", null);
        // addParaMap.put("DATA_TABLE_NAME",dataTablename );
        sqlDaoMapper.addXmlSql(addParaMap);
    }


}
